package com.lanmon.service.auth;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.lanmon.base.HibernateService;
import com.lanmon.base.PageEntity;
import com.lanmon.dao.auth.ComUserDao;
import com.lanmon.entity.ComUser;

@Service
@Component("comUserService")
public class ComUserService extends HibernateService<ComUser, Integer> {
	private ComUserDao comUserDao;
	
	public ComUserDao getComUserDao() {
		return comUserDao;
	}
	
	@Autowired
	public void setComUserDao(ComUserDao comUserDao) {
		super.setHibernateDao(comUserDao);
		this.comUserDao = comUserDao;
	}
	
	public ComUserService() {
		super();
	}
	
	public ComUser findlogin(ComUser user) throws Exception {
		user.setIsvalid(1);
		List<ComUser> userinfos = findByExample(user);
		if (userinfos != null && !userinfos.isEmpty()) {
			user = userinfos.get(0);
		}
		else {
			user = null;
		}
		return user;
	}
	
	/**
	 * 列表查询，分页
	 * 
	 * @param entity
	 * @param user
	 *            封装查询参数
	 */
	public void findFetchPage(PageEntity<ComUser> entity, ComUser user) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ComUser.class);
		if (!StringUtils.isEmpty(user.getUname()))
			criteria.add(Restrictions.like("uname", "%" + user.getUname() + "%"));
		if (!StringUtils.isEmpty(user.getUsername()))
			criteria.add(Restrictions.like("username", "%" + user.getUsername() + "%"));
		if(!StringUtils.isEmpty(user.getOrganization())&&!StringUtils.isEmpty(user.getOrganization().getOrgid())){
			criteria.add(Restrictions.eq("organization.orgid", user.getOrganization().getOrgid()));
		}
		findFetchPage(criteria, entity);
	}
}
